package com.cnplan.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.cnplan.auth.utils.PasswordUtils;
import com.cnplan.core.Constant;
import com.cnplan.core.Result;
import com.cnplan.core.ResultGenerator;
import com.cnplan.entity.*;
import com.cnplan.mapper.*;
import com.cnplan.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2018/4/25.
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private SystemUserMapper systemUserMapper;

    @Autowired
    private SystemRoleMapper systemRoleMapper;


    // 系统用户相关操作
    public SystemUser getSystemUser(SystemUser user){
        return systemUserMapper.selectOne(user);
    }

    public SystemUser getSystemUserByUsername(String username) {
        SystemUser user = new SystemUser();
        user.setUserName(username);
        return systemUserMapper.selectOne(user);
    }

    public List<SystemUser> listSystemUser(Wrapper<SystemUser> ew){
        return systemUserMapper.selectList(ew.orderBy("active", false)
                .orderBy("id",true));
    }

    public List<SystemUser> listSystemUser(Wrapper<SystemUser> ew, String keyword){
        if (keyword == null || "".equals(keyword.trim())) {
            return systemUserMapper.selectList(ew.orderBy("active",false)
                    .orderBy("id", true));
        } else {
            return systemUserMapper.selectList(ew.andNew().like("user_name", keyword)
                    .or().like("real_name", keyword)
                    .or().like("dept_name",keyword)
                    .or().like("email",keyword)
                    .or().like("phone",keyword)
                    .orderBy("active",false)
                    .orderBy("id", true));
        }
    }

    public Result insertSystemUser(SystemUser user){
        if(systemUserMapper.insert(user) == 1){
            return ResultGenerator.genSuccessResult(user.getId().toString());
        }else{
            return ResultGenerator.genFailResult("新增用户失败");
        }
    }

    public Result updateSystemUser(SystemUser user){
        if(systemUserMapper.updateById(user) == 1){
            return ResultGenerator.genSuccessResult(user.getId().toString());
        }else{
            return ResultGenerator.genFailResult("更新用户失败");
        }
    }

    public Result deleteSystemUser(Long id){
        if(systemUserMapper.deleteById(id)==1){
            return ResultGenerator.genSuccessResult(id);
        }else{
            return ResultGenerator.genFailResult("删除用户失败");
        }
    }

    public Result modifyPassword(Integer type, Long id, String oriPassword, String newPassword){
        SystemUser systemUser = systemUserMapper.selectById(id);

        if (type == Constant.RESET_PASSWORD) {
            systemUser.setUserPassword(newPassword);
            PasswordUtils.encryptPassword(systemUser);
            if (systemUserMapper.updateById(systemUser) == 1) {
                return ResultGenerator.genSuccessResult(id.toString());
            } else {
                return ResultGenerator.genFailResult("重置密码失败");
            }
        } else if (type == Constant.CHANGE_PASSWORD) {
            if (!PasswordUtils.checkPassword(systemUser, oriPassword)) {
                return ResultGenerator.genFailResult("原密码错误");
            }
            systemUser.setUserPassword(newPassword);
            PasswordUtils.encryptPassword(systemUser);
            if (systemUserMapper.updateById(systemUser) == 1) {
                return ResultGenerator.genSuccessResult(id.toString());
            } else {
                return ResultGenerator.genFailResult("修改密码失败");
            }
        } else {
            return ResultGenerator.genFailResult("修改密码失败");
        }
    }

    public Result resetUserRoles(Long userId, Long... roleIds){
        // 首先获取用户
        SystemUser temp = new SystemUser();
        temp.setId(userId);
        SystemUser systemUser = systemUserMapper.selectOne(temp);
        if(systemUser == null){
            return ResultGenerator.genFailResult("获取用户信息失败");
        }

        // 删除已有角色
        systemUserMapper.unlinkAllUserRoles(userId);
        // 重新建立角色链接
        for(Long roleId : roleIds){
            systemUserMapper.linkUserRole(userId, roleId);
        }
        return ResultGenerator.genSuccessResult(systemUser.getId().toString());
    }

    // 角色操作
    public SystemRole getSystemRole(SystemRole systemRole){
        return systemRoleMapper.selectOne(systemRole);
    }

    public List<SystemRole> listSystemRole(Wrapper<SystemRole> ew){
        return systemRoleMapper.selectList(ew.orderBy("active", false)
                .orderBy("id",true));
    }

    public List<SystemRole> listSystemRole(Wrapper<SystemRole> ew, String keyword){
        if (keyword == null || "".equals(keyword.trim())) {
            return systemRoleMapper.selectList(ew.orderBy("active",false)
                    .orderBy("id", true));
        } else {
            return systemRoleMapper.selectList(ew.andNew().like("role_name", keyword)
                    .or().like("role_desc",keyword)
                    .orderBy("id", true));
        }
    }

    public Result insertSystemRole(SystemRole systemRole){
        if(systemRoleMapper.insert(systemRole) == 1){
            return ResultGenerator.genSuccessResult(systemRole.getId().toString());
        }else{
            return ResultGenerator.genFailResult("新增角色失败");
        }
    }

    public Result updateSystemRole(SystemRole systemRole){
        if(systemRoleMapper.updateById(systemRole) == 1){
            return ResultGenerator.genSuccessResult(systemRole.getId().toString());
        }else{
            return ResultGenerator.genFailResult("更新角色失败");
        }
    }

    public Result deleteSystemRole(Long id){
        if(systemRoleMapper.deleteById(id)==1){
            return ResultGenerator.genSuccessResult(id);
        }else{
            return ResultGenerator.genFailResult("删除角色失败");
        }
    }

    public List<SystemRole> listRoleByUserId(Long userId){
        return systemRoleMapper.listRoleByUserId(userId);
    }

}
